For this beginner-mid level project I used a Covid dataset as my base to use queries to explore the information that the dataset contains. Skills used: Joins, CTE’s, Temp Tables, Windows Functions, Aggregate Functions, Creating Views, Converting Data Types.
In order to use SQL databases, you must establish connection with Microsoft SQL Server. One way to do that, is using “dbConnect”:
Let’s take a first look into our data:
SELECT *
FROM "Portfolio"."dbo"."covid_death"
WHERE continent IS NOT NULL
ORDER BY 3,4Select the data the we are going to be starting with
SELECT location, date, total_cases, new_cases, total_deaths, population
FROM "Portfolio"."dbo"."covid_death"
WHERE continent IS NOT NULL
ORDER BY 1,2How many cases there is in this country and how many deaths they have per the total number of cases (in percentage). I can say that this shows likelihood of dying if you contract covid in your country. For this example I will use “Brazil” as the country that will be looking.
SELECT location, date, total_cases, total_deaths, (total_deaths/total_cases)*100 AS DeathPercentage
FROM "Portfolio"."dbo"."covid_death"
WHERE location LIKE '%Brazil%'
AND continent IS NOT NULL
ORDER BY 1,2Shows what percentage of population that was infected with Covid in Brazil.
SELECT location, date, Population, total_cases, ROUND((total_cases/population),5)*100 AS PercentPopulationInfected
FROM "Portfolio"."dbo"."covid_death"
WHERE location LIKE '%Brazil%'
AND continent IS NOT NULL
ORDER BY 1,2SELECT location, Population, MAX(total_cases) AS HighestInfectionCount, MAX(ROUND((total_cases/population),4))*100 AS PercentPopulationInfected
FROM "Portfolio"."dbo"."covid_death"
GROUP BY location, population
ORDER BY PercentPopulationInfected DESCSELECT location, Population, MAX(cast(Total_deaths as int)) as TotalDeathCount
FROM "Portfolio"."dbo"."covid_death"
WHERE continent IS NOT NULL
GROUP BY location, Population
ORDER BY TotalDeathCount DESCSELECT SUM(new_cases) as total_cases, SUM(cast(new_deaths as int)) as total_deaths, SUM(cast(new_deaths as int))/SUM(New_Cases)*100 as DeathPercentage
FROM "Portfolio"."dbo"."covid_death"
WHERE continent IS NOT NULL
ORDER BY 1,2| total_cases | total_deaths | DeathPercentage |
|---|---|---|
| 4355726910 | 59296330 | 1.361342 |
Shows Percentage of Population that has received at least one Covid Vaccine, during the pandemic.
SELECT dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(CAST(vac.new_vaccinations AS float)) OVER (Partition BY dea.Location ORDER BY dea.location, dea.Date) AS RollingPeopleVaccinated
FROM "Portfolio"."dbo"."covid_death" dea
JOIN "Portfolio"."dbo"."covid_vaccinations" vac
ON dea.location = vac.location
AND dea.date = vac.date
WHERE dea.continent IS NOT NULL
AND dea.location LIKE '%Brazil%'
ORDER BY 2,3Using CTE to perform Calculation on Partition By in previous query
With PopvsVac (Continent, Location, Date, Population, New_Vaccinations, RollingPeopleVaccinated)
as
(
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(CAST(vac.new_vaccinations AS float)) OVER (Partition by dea.Location Order by dea.location, dea.Date) as RollingPeopleVaccinated
From Portfolio..covid_death dea
Join Portfolio..covid_vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
AND dea.location like '%Brazil%'
)
Select *, ROUND((RollingPeopleVaccinated/Population),6)*100 AS PercentagePopVaccinated
From PopvsVacUsing Temp Table to perform Calculation on Partition By in previous query
DROP Table if exists #PercentPopulationVaccinated
Create Table #PercentPopulationVaccinated
(
Continent nvarchar(255),
Location nvarchar(255),
Date datetime,
Population numeric,
New_vaccinations numeric,
RollingPeopleVaccinated numeric
)
Insert into #PercentPopulationVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(CAST(vac.new_vaccinations AS float)) OVER (Partition by dea.Location Order by dea.location, dea.Date) as RollingPeopleVaccinated
From Portfolio..covid_death dea
Join Portfolio..covid_vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
Select *, ROUND((RollingPeopleVaccinated/Population),4)*100 AS PctPopVacOverTime
From #PercentPopulationVaccinated
Where continent is not null
AND location like '%states%'